#ifndef __UDP_SERVER_HPP__
#define __UDP_SERVER_HPP__

#include <iostream>
#include <string>
#include <memory>
#include <strings.h>
#include <string.h>
#include <error.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "Log.hpp"
#include "Common.hpp"
#include "InetAddr.hpp"

const static int gsockfd = -1;
// const static std::string gdefaultip = "127.0.0.1"; //表示本地主机
const static uint16_t gdefaultport = 8080;

using namespace LogMudule;



class UdpServer
{
public:
    UdpServer(uint16_t port = gdefaultport)
        :_sockfd(gsockfd)
        ,_addr(port)
        ,_isrunning(false)
    {}

    void InitServer()
    {
        // 1. 创建Socket
        _sockfd = ::socket(AF_INET, SOCK_DGRAM, 0);
        if (_sockfd < 0)
        {
            LOG(LogLevel::FATAL) << "socket: " << strerror(errno);
            Die(SOCKET_ERR);
        }
        LOG(LogLevel::INFO) << "socket success, socket is : " << _sockfd;

        // 2. 填充网络信息，并bind绑定
        // 2.1 有没有把socket信息，设置到内核里
        // struct sockaddr_in local;
        // bzero(&local,sizeof(local));
        // local.sin_family = AF_INET;
        // local.sin_port = ::htons(_port); //要被发送给对方的，即要发到网络中---->把主机序列转成网络序列
        // // local.sin_addr.s_addr = ::inet_addr(_ip.c_str());//1. stirng ip->4bytes 2.转成网络序列
        // local.sin_addr.s_addr = INADDR_ANY; // 任何主机的ip都照样收消息


        // 2.2 bind ： 设置内核
        // int n = ::bind(_sockfd,CONV(&local),sizeof(local));
        int n = ::bind(_sockfd,_addr.NetAddr(),_addr.NetAddrLen());
        if( n < 0)
        {
            LOG(LogLevel::FATAL) << "bind: " << strerror(errno);
            Die(BIND_ERR);
        }
        LOG(LogLevel::INFO) << "bind success";
    }
    void Start()
    {
        _isrunning = true;
        while (true)
        {
            char inbuffer[1024]; // string
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer);

            ssize_t n = ::recvfrom(_sockfd, inbuffer,sizeof(inbuffer)-1, 0,CONV(&peer),&len);
            if(n > 0)
            {
                //1. 消息内容 && 2.谁给我发的
                // uint16_t clientport = ::ntohs(peer.sin_port); // 把网络序列转成主机序列
                // std::string clientip = ::inet_ntoa(peer.sin_addr); 

                InetAddr client(peer);
                inbuffer[n] = 0;

                std::string clentinfo = client.Ip() + ":" +std::to_string(client.Port())+" # " + inbuffer;

                LOG(LogLevel::DEBUG) <<clentinfo;
                
                std::string echo_string = "echo# ";
                echo_string += inbuffer;

                ::sendto(_sockfd,echo_string.c_str(),echo_string.size(),0,CONV(&peer),sizeof(peer));
            }
        }
        _isrunning = false;
    }

    ~UdpServer() 
    {
        if(_sockfd > gsockfd)
        {
            ::close(_sockfd);
        }
    }

private:
    int _sockfd;
    InetAddr _addr;
    // uint16_t _port;  // 服务器未来的端口号
    // std::string _ip; // 服务器所对应的IP
    bool _isrunning; // 表示服务器
};

#endif